<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>盒子水平居中</title>
    <style>
        .box {
            position: relative;
            margin-top: 50px;
            height:100px; 
            background-color: skyblue;
        }
        .center-box {
            width: 100px;
            height: 100px;
            background-color: blue;
            font: bold 40px/100px "arial";
			text-align: center;
        }

        /* 前提子盒子宽度小于父盒子宽度 */
        #box1 {
            margin: 0 auto;
        }

        /* 子绝父相 */
        #box2 {
            position: absolute;
            left: 50%;
            /* 子盒子宽度的一半 */
            margin-left: -50px;
        }

        /* 子绝父相，水平平移*/
        #box3 {
            position: absolute;
            left: 50%;
            /* 子盒子宽度的一半 */
            transform: translateX(-50%);
        }
    </style>
</head>
<body>
    <div class="box">
        <div id="box1" class="center-box">
            1
        </div>
    </div>
    <div class="box">
        <div id="box2" class="center-box">
            2
        </div>
    </div>
    <div class="box">
        <div id="box3" class="center-box">
            3
        </div>
    </div>
</body>
</html>